package Commer;

import number.Input;

import javax.swing.*;
import java.awt.*;
import java.io.FileWriter;
import java.io.IOException;


public class MyFrame extends JFrame {
    private static JTextField[] textFields = new JTextField[4];
    private static JTextArea[] textAreas = new JTextArea[4];

    public MyFrame() {
        // 设置窗口标题
        setTitle("输入整数值");
        setFont(new Font("微软雅黑", 0, 14));
        // 窗口大小不可变
        setResizable(false);

        // 设置窗口大小
        setSize(400, 200);

        // 设置关闭操作
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 创建布局管理器
        setLayout(new GridLayout(5, 2)); // 5行2列
        // 添加文本输入框
        for (int i = 0; i < 4; i++) {
            textFields[i] = new JTextField();
            textAreas[i] = new JTextArea();
            //textAreas不可输入
            textAreas[i].setEditable(false);
            add(textAreas[i]);
            add(textFields[i]);
        }
        nextint();
        JLabel label = new JLabel("To make up the number");
        add(label);


        JButton saveButton = new JButton("保存");
        saveButton.setFont(new Font("微软雅黑", 0, 24));
        saveButton.addActionListener(e -> saveValues());
        add(saveButton);
        // 显示窗口
        setVisible(true);
    }

    public void nextint() {
        textFields[0].setFont(new java.awt.Font("微软雅黑", 0, 24));
        textFields[1].setFont(new java.awt.Font("微软雅黑", 0, 24));
        textFields[2].setFont(new java.awt.Font("微软雅黑", 0, 24));
        textFields[3].setFont(new java.awt.Font("微软雅黑", 0, 24));
        textAreas[0].setFont(new java.awt.Font("微软雅黑", 0, 24));
        textAreas[1].setFont(new java.awt.Font("微软雅黑", 0, 24));
        textAreas[2].setFont(new java.awt.Font("微软雅黑", 0, 24));
        textAreas[3].setFont(new java.awt.Font("微软雅黑", 0, 24));

        textAreas[0].setText("输入私信第一个数：");
        textAreas[1].setText("输入私信第二个数：");
        textAreas[2].setText("输入头像第三个数：");
        textAreas[3].setText("输入头像第四个数：");

        textFields[0].setText(String.valueOf(Input.getNumber_1a()));
        textFields[1].setText(String.valueOf(Input.getNumber_1b()));
        textFields[2].setText(String.valueOf(Input.getNumber_2a()));
        textFields[3].setText(String.valueOf(Input.getNumber_2b()));

    }

    private void saveValues() {
        try (FileWriter writer = new FileWriter("Test.txt")) {
            for (JTextField textField : textFields) {
                String value = textField.getText();
                if (value.isEmpty()) {
                    JOptionPane.showMessageDialog(this, "请输入所有值", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                int intValue = Integer.parseInt(value);
                writer.write(intValue + "\n");
            }
            JOptionPane.showMessageDialog(this, "保存成功", "成功", JOptionPane.INFORMATION_MESSAGE);
            //点击成功后，关闭窗口
            this.dispose();
        } catch (IOException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this, "保存失败: " + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "请输入有效的整数", "错误", JOptionPane.ERROR_MESSAGE);
        }
    }
}
